package com.edvinli.controller;

import com.edvinli.entity.PageBean;
import com.edvinli.entity.Result;
import com.edvinli.entity.Student;
import com.edvinli.entity.StudentQueryParam;
import com.edvinli.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学生模块分页功能
     * @param
     * @return
     */
    @GetMapping("/students")
    public Result page(StudentQueryParam param){
        log.info("分页信息:{},{},{},{},{}", param.getName(),param.getDegree(),param.getClazzId(),
                                            param.getPage(),param.getPageSize());
        PageBean pageBean = studentService.page(param);

        return Result.success(pageBean);
    }

    /**
     * 新增学生
     * @param student
     * @return
     */
    @PostMapping("/students")
    public Result create(@RequestBody Student student){
        log.info("新增学员:{}", student);
        studentService.create(student);

        return Result.success();
    }

    /**
     * 通过id查询学员信息回显
     * @param id
     * @return
     */
    @GetMapping("/students/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("通过id查询学员信息:{}", id);
        Student student = studentService.getById(id);

        return Result.success(student);
    }

    /**
     * 修改学员信息
     * @param student
     * @return
     */
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("修改学员信息:{}", student);
        studentService.update(student);

        return Result.success();
    }

    /**
     * 根据id批量删除学员
     * @param ids
     * @return
     */
    @DeleteMapping("/students/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids){
        log.info("删除学员:{}",ids);
        studentService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 违纪记录
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/students/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id,@PathVariable Integer score){
        log.info("违纪记录:{},{}",id, score);
        studentService.violate(id, score);

        return Result.success();
    }
}
